'''快捷启动,用tkinter作界面,deepSeek写完我改装.

  By Setycyas @2025-02-10
'''

import os
import json
import tkinter as tk
from tkinter import messagebox
import subprocess

class MyLauncher:

  data = None # json读取的{}
  root = None # tk窗口
  btn = None # 按钮
  lbox = None # 列表框
  
  def __init__(self):
    self.readData() # 读取json数据
    self.setTk() # 制作tkinter窗口
    return
    
  def readData(self):
    with open('data.json', 'rb') as f:
      content = f.read()
      string = content.decode('utf8')
      self.data = json.loads(string)
    return
  
  def setTk(self):
    self.root = tk.Tk() # 创建主窗口
    self.root.title("我的快捷运行框")
    self.lbox = tk.Listbox(self.root) # 创建列表框
    self.lbox.pack(padx=20, pady=20)
    # 向列表框中添加一些项
    for k in self.data:
      self.lbox.insert(tk.END, k)
    # 创建一个按钮，点击时调用show_selection函数
    self.btn = tk.Button(self.root, text="运行选中的程序", command=self.run_selection)
    self.btn.pack(pady=10)

  def run_selection(self):
    '''运行选中的程序'''
    # 获取当前选中的项
    selected_indices = self.lbox.curselection()
    if selected_indices:  # 如果有选中的项
      selected_item = self.lbox.get(selected_indices[0])
      # 运行并独立于本脚本
      subprocess.Popen([self.data[selected_item]], start_new_session=True)
    else:
      messagebox.showinfo("提示", "请先选择一个项目")
    return
    
  def run(self):
    # 运行主循环
    self.root.mainloop()  

## 运行
if __name__ == '__main__':
  MyLauncher().run()
